from django.db import models
from django.contrib.auth.models import User


class PsychologicalScale(models.Model):
    """心理量表库"""
    scale_name = models.CharField(
        max_length=100, 
        verbose_name='量表名称',
        db_comment='心理量表名称(SDS/SAS/PHQ-9/GAD-7等)'
    )
    scale_code = models.CharField(
        max_length=50, 
        unique=True, 
        verbose_name='量表编码',
        db_comment='量表唯一编码'
    )
    description = models.TextField(
        verbose_name='量表描述',
        db_comment='量表作用和适用场景说明'
    )
    category = models.CharField(
        max_length=50, 
        verbose_name='量表分类',
        db_comment='量表分类(抑郁/焦虑/压力/等)'
    )
    total_questions = models.IntegerField(
        verbose_name='题目总数',
        db_comment='量表题目总数'
    )
    scale_content = models.JSONField(
        verbose_name='量表内容',
        db_comment='量表题目和选项JSON数据'
    )
    scoring_rules = models.JSONField(
        verbose_name='计分规则',
        db_comment='量表计分方法和规则JSON'
    )
    interpretation_rules = models.JSONField(
        verbose_name='结果解读',
        db_comment='分数区间对应解读规则JSON'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='量表是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_psychological_scale'
        db_table_comment = '心理量表库'
        verbose_name = '心理量表'
        verbose_name_plural = '心理量表'

    def __str__(self):
        return self.scale_name


class AssessmentRecord(models.Model):
    """测评记录表"""
    STATUS_CHOICES = [
        ('pending', '未完成'),
        ('completed', '已完成'),
        ('abandoned', '已放弃'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    scale = models.ForeignKey(
        PsychologicalScale, 
        on_delete=models.CASCADE, 
        verbose_name='量表',
        db_comment='关联心理量表ID'
    )
    status = models.CharField(
        max_length=20, 
        choices=STATUS_CHOICES, 
        default='pending',
        verbose_name='状态',
        db_comment='测评状态'
    )
    answers = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='答案',
        db_comment='用户答案JSON数据'
    )
    total_score = models.FloatField(
        null=True, 
        blank=True, 
        verbose_name='总分',
        db_comment='测评总分'
    )
    result_interpretation = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='结果解读',
        db_comment='测评结果解读和建议'
    )
    result_level = models.CharField(
        max_length=50, 
        null=True, 
        blank=True, 
        verbose_name='结果等级',
        db_comment='结果等级(轻度/中度/重度)'
    )
    start_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='开始时间',
        db_comment='测评开始时间'
    )
    complete_time = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='完成时间',
        db_comment='测评完成时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_assessment_record'
        db_table_comment = '测评记录表'
        verbose_name = '测评记录'
        verbose_name_plural = '测评记录'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.scale.scale_name} - {self.start_time}'
